/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sxtyxt.online.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.sxtyxt.online.SystemEnv;

public class DbHelper extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static DbHelper instance;



    public static final String LOOK_HISTORY_LIST_CREATE = "CREATE TABLE "
            + LookHistoryListDao.TABLE_NAME + " ("
            + LookHistoryListDao.COLUMN_NAME_GOODS_ID + " TEXT, "
            + LookHistoryListDao.COLUMN_NAME_GOODS_IMG + " TEXT, "
            + LookHistoryListDao.COLUMN_NAME_GOODS_NAME + " TEXT, "
            + LookHistoryListDao.COLUMN_NAME_GOODS_PRICE + " TEXT, "
            + LookHistoryListDao.COLUMN_NAME_CREATE + " TEXT, "
            + LookHistoryListDao.COLUMN_NAME_EXT1 + " TEXT, "
            + LookHistoryListDao.COLUMN_NAME_EXT2 + " TEXT, "
            + LookHistoryListDao.COLUMN_NAME_EXT3 + " TEXT, "
            + LookHistoryListDao.COLUMN_NAME_ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT );";

    private DbHelper(Context context) {
        super(context, getUserDatabaseName(), null, DATABASE_VERSION);
    }

    public static DbHelper getInstance(Context context) {
        if (instance == null) {
            instance = new DbHelper(context.getApplicationContext());
        }
        return instance;
    }

    public static void ReSetDB() {
        instance = null;
    }

    private static String getUserDatabaseName() {
        String userid = "";
        if (null != SystemEnv.getUser()) {
            userid = SystemEnv.getUser().getPkId();
        }
        return userid + "leimingtech.db";
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(LOOK_HISTORY_LIST_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(LookHistoryListDao.DROP);
        db.execSQL(LOOK_HISTORY_LIST_CREATE);


    }

    public void closeDB() {
        if (instance != null) {
            try {
                SQLiteDatabase db = instance.getWritableDatabase();
                db.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            instance = null;
        }
    }

}
